clear

global estout_options          label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(ll bic r2_p N N_clust xtercsamp, fmt(0 0 2 0 0) labels("Log Likelihood" "BIC" "Pseudo $ R^2 $" "N" "N of elections" "Income tercile(s)")) cells( " b( fmt(2) ) p(fmt(2)) " ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )
global estout_options_w_se     label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(ll bic r2_p N N_clust xtercsamp, fmt(0 0 2 0 0) labels("Log Likelihood" "BIC" "Pseudo $ R^2 $" "N" "N of elections" "Income tercile(s)")) cells( " b( fmt(2) ) se(fmt(2)) p(fmt(2))" ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )
global estout_options_ols_w_se label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(r2 N xtercsamp, fmt(2 0 0) labels("$ R^2 $" "N" "Income tercile(s)")) cells( " b( fmt(2) ) se(fmt(2)) p(fmt(2))" ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )

do "$scriptsdir/ca_preparecdf.do"


clear
// Load election dates
insheet using "$sourcedatadir/wtid/view_election.csv"
keep if country_name == "Canada"
keep if election_type == "parliament"
rename country_name country
rename election_date election_date_str
gen election_date = date(election_date_str, "YMD")
format election_date %td
bys election_date: keep if _n == 1
keep country election_date
gen year = year(election_date)
save "$gendatadir/ca_election_dates.dta", replace


do "$scriptsdir/ca_prepareincome_t5m.do"

gen logtenure = log(incumbent_tenure)

*rename year effective_year
*merge 1:m effective_year using "ca/CES/canada cdf processed.dta", nogenerate update
merge 1:m year using "$gendatadir/ca_cdf.dta", nogenerate update
drop if election_date == .
*drop year
*rename effective_year year

sort year

// Create weights that weight each election equally
bys election_date: egen natlweight_sum = total(natlweight)
gen natlweight_eqelec = 10000*natlweight/natlweight_sum 


label variable vote_ce 				"$ VoteIncumbent_{i,t} $"
label variable inc_mean_xx_g1		"$ Growth^{M}_{t} $"
label variable incumbent_tenure		"$ Tenure_{t} $"
label variable logtenure			"$ \log Tenure_{t} $"
label variable pid_ce 				"$ ProPartyID_{i,t} $"
label variable inc_quin1_xx_g1		"$ Growth^{Q1-2}_{t} $"
label variable inc_quin2_xx_g1		"$ Growth^{Q2}_{t} $"
label variable inc_quin3_xx_g1		"$ Growth^{Q3}_{t} $"
label variable inc_quin4_xx_g1		"$ Growth^{Q4}_{t} $"
label variable inc_top5_xx_g1		"$ Growth^{T5M}_{t} $"
label variable inc_quin12_xx_g1		"$ Growth^{Q1-2}_{t} $"
*label variable union2				"$ Union_{i,t} $"
*label variable gpi					"$ PolInfo_{i,t} $"
*label variable camppi				"$ camppi_{i,t} $"


// Drop abstainers
keep if fedvote != 0


probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenure
probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLow
probit vote_ce logtenure pid_ce inc_quin1_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1Low
probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_quin1_xx_g1 [pweight=natlweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store MeanQ1Low
probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMid
probit vote_ce logtenure pid_ce inc_quin3_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredQ3Mid
probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_quin3_xx_g1 [pweight=natlweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store MeanQ3Mid

estout  BartelsLogtenure PreferredLow PreferredQ1Low PreferredMid PreferredQ3Mid		using "$texdir/ca_cbevcomp_tableA_`inccalc'.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_xx_g1 inc_quin1_xx_g1 inc_quin3_xx_g1 inc_top5_xx_g1)


label variable inc_dist_quin1_g1	"$ Growth^{P20}_{t} $"


/// Now carry on with the final data setup, producing a fuller set of robustness results
*gen            share5_x_T5g1 =	inc_t5_share_at * inc_top5_xx_g1
gen            share5_x_T5g1 =	share5 * inc_top5_xx_g1
label variable inc_t5_share_at	"$ Share^{T5}_t $"
label variable share5			"$ Share^{T5}_t $"
label variable share5_x_T5g1	"$ Share^{T5}_{t} \times Growth^{T5M}_{t} $"


//// Estimate some aggregated models by ols - so excluding PID.
preserve
collapse (mean) vote_ce incumbent_tenure incmbt logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 share5 [pweight=natlweight_eqelec], by(year)
reg vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1
reg vote_ce logtenure inc_mean_xx_g1 inc_top5_xx_g1
reg vote_ce logtenure inc_mean_xx_g1 inc_top5_xx_g1 incmbt
reg pid_ce logtenure inc_mean_xx_g1 inc_top5_xx_g1
estadd local xtercsamp = "All"
estimates store PIDOLSAll
restore

preserve
collapse (mean) vote_ce incumbent_tenure incmbt logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 0, by(year)
reg vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1
reg vote_ce logtenure inc_mean_xx_g1 inc_top5_xx_g1
reg vote_ce logtenure inc_mean_xx_g1 inc_top5_xx_g1 incmbt
reg pid_ce logtenure inc_mean_xx_g1 inc_top5_xx_g1
estadd local xtercsamp = "Low"
estimates store PIDOLSLow
restore

preserve
collapse (mean) vote_ce incumbent_tenure incmbt logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 1, by(year)
reg vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1
reg vote_ce logtenure inc_mean_xx_g1 inc_top5_xx_g1
reg vote_ce logtenure inc_mean_xx_g1 inc_top5_xx_g1 incmbt
reg pid_ce logtenure inc_mean_xx_g1 inc_top5_xx_g1
estadd local xtercsamp = "Mid"
estimates store PIDOLSMid
restore

estout  PIDOLSAll PIDOLSLow PIDOLSMid		using "$texdir/ca_pid.tex",	$estout_options_ols_w_se order(logtenure inc_mean_xx_g1 inc_top5_xx_g1)



// An identifier so we can summarize based on year not individual-level sample size
bys year: gen firstob = 1 if _n == 1
replace firstob = 0 if firstob == .


//// Perform various CBEV tests for the models above, and output the results to text files for direct inclusion into ".tex" tables.
estimates restore BartelsLogtenure
test inc_top5_xx_g1
local pstat =  string(r(p), "%9.2fc")
*local chi2stat = string(r(chi2), "%9.2fc")
capture {
  file open  pstatfile using "$texdir/ca_BartelsLogtenure_pstat.tex", write replace
  file write pstatfile "`pstat'"
  file close pstatfile
}

estimates restore PreferredLow
*quietly summ share5 if e(sample)
*local mean_share5 = r(mean)/100
*quietly summ share10 if e(sample)
*local mean_share10 = r(mean)/100
*test (inc_meanb80_xx_g1/(1-`mean_share10')) = (inc_ll_95_xx_g1/`mean_share5')
test inc_top5_xx_g1
local pstat =  string(r(p), "%9.2fc")
*local chi2stat = string(r(chi2), "%9.2fc")
capture {
  file open  pstatfile using "$texdir/ca_PreferredLow_pstat.tex", write replace
  file write pstatfile "`pstat'"
  file close pstatfile
  *file open  chi2statfile using "ca/ca_PreferredLow_chi2stat.tex", write replace
  *file write chi2statfile "`chi2stat'"
  *file close chi2statfile
}

estimates restore PreferredQ1Low
*quietly summ share5 if e(sample)
*local mean_share5 = r(mean)/100
*quietly summ share10 if e(sample)
*local mean_share10 = r(mean)/100
*test (inc_ul_quin1_xx_g1/(1-`mean_share10')) = (inc_ll_95_xx_g1/`mean_share5')
*test inc_ul_quin1_xx_g1 = inc_ll_95_xx_g1
test inc_quin1_xx_g1
local pstat =  string(r(p), "%9.2fc")
*local chi2stat = string(r(chi2), "%9.2fc")
capture {
  file open  pstatfile using "$texdir/ca_PreferredQ1Low_pstat.tex", write replace
  file write pstatfile "`pstat'"
  file close pstatfile
  *file open  chi2statfile using "ca/ca_PreferredQ1Low_chi2stat.tex", write replace
  *file write chi2statfile "`chi2stat'"
  *file close chi2statfile
}

estimates restore PreferredQ3Mid
*quietly summ share5 if e(sample)
*local mean_share5 = r(mean)/100
*quietly summ share10 if e(sample)
*local mean_share10 = r(mean)/100
*test (inc_ul_quin1_xx_g1/(1-`mean_share10')) = (inc_ll_95_xx_g1/`mean_share5')
*test inc_ul_quin1_xx_g1 = inc_ll_95_xx_g1
test inc_quin3_xx_g1
local pstat =  string(r(p), "%9.2fc")
*local chi2stat = string(r(chi2), "%9.2fc")
capture {
  file open  pstatfile using "$texdir/ca_PreferredQ3Mid_pstat.tex", write replace
  file write pstatfile "`pstat'"
  file close pstatfile
  *file open  chi2statfile using "ca/ca_PreferredQ1Low_chi2stat.tex", write replace
  *file write chi2statfile "`chi2stat'"
  *file close chi2statfile
}

estimates restore PreferredMid
*quietly summ share5 if e(sample)
*local mean_share5 = r(mean)/100
*quietly summ share10 if e(sample)
*local mean_share10 = r(mean)/100
*test (inc_meanb80_xx_g1/(1-`mean_share10')) = (inc_ll_95_xx_g1/`mean_share5')
test inc_top5_xx_g1
local pstat =  string(r(p), "%9.2fc")
*local chi2stat = string(r(chi2), "%9.2fc")
capture {
  file open  pstatfile using "$texdir/ca_PreferredMid_pstat.tex", write replace
  file write pstatfile "`pstat'"
  file close pstatfile
  *file open  chi2statfile using "ca/ca_PreferredMid_chi2stat.tex", write replace
  *file write chi2statfile "`chi2stat'"
  *file close chi2statfile
}



//// Section of models that we present in supplementary material


probit vote_ce incumbent_tenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store Bartels

estout  Bartels BartelsLogtenure		using "$texdir/ca_cbevcomp_tableB.tex",	$estout_options_w_se order(incumbent_tenure logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1)


probit vote_ce logtenure inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureNoPID
probit vote_ce logtenure inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowNoPID
probit vote_ce logtenure inc_quin1_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1LowNoPID
probit vote_ce logtenure inc_mean_xx_g1 inc_quin1_xx_g1 [pweight=natlweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store MeanQ1LowNoPID
probit vote_ce logtenure inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidNoPID
probit vote_ce logtenure inc_quin3_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredQ3MidNoPID
probit vote_ce logtenure inc_mean_xx_g1 inc_quin3_xx_g1 [pweight=natlweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store MeanQ3MidNoPID

estout  BartelsLogtenureNoPID PreferredLowNoPID PreferredQ1LowNoPID PreferredMidNoPID PreferredQ3MidNoPID		using "$texdir/ca_cbevcomp_tableC.tex",	$estout_options_w_se order(logtenure inc_mean_xx_g1 inc_quin1_xx_g1 inc_quin3_xx_g1 inc_top5_xx_g1)


probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if year > 1983, vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureT5M
probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 0 & year > 1983, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowT5M
probit vote_ce logtenure pid_ce inc_quin1_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 0 & year > 1983, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1LowT5M
probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_quin1_xx_g1 [pweight=natlweight_eqelec] if income3 == 0 & year > 1983, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store MeanQ1LowT5M
probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 1 & year > 1983, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidT5M
probit vote_ce logtenure pid_ce inc_quin3_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 1 & year > 1983, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredQ3MidT5M
probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_quin3_xx_g1 [pweight=natlweight_eqelec] if income3 == 1 & year > 1983, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store MeanQ3MidT5M

estout  BartelsLogtenureT5M PreferredLowT5M PreferredQ1LowT5M PreferredMidT5M PreferredQ3MidT5M		using "$texdir/ca_cbevcomp_tableD.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_xx_g1 inc_quin1_xx_g1 inc_quin3_xx_g1 inc_top5_xx_g1)


probit vote_ce logtenure pid_ce inc_quin1_xx_g1  inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store OwnMeanLow
probit vote_ce logtenure pid_ce inc_quin3_xx_g1  inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store OwnMeanMid
probit vote_ce logtenure pid_ce inc_quin12_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ12Low

estout  OwnMeanLow OwnMeanMid PreferredQ12Low		using "$texdir/ca_cbevcomp_tableE.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_xx_g1 inc_quin1_xx_g1 inc_quin12_xx_g1 inc_quin3_xx_g1 inc_top5_xx_g1)


probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 share5 share5_x_T5g1 [pweight=natlweight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureS5
probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 share5 share5_x_T5g1 [pweight=natlweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowS5
probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 share5 share5_x_T5g1 [pweight=natlweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidS5

estout  BartelsLogtenureS5 PreferredLowS5 PreferredMidS5		using "$texdir/ca_cbevcomp_tableF.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_xx_g1 share5 inc_top5_xx_g1 share5_x_T5g1)


probit vote_ce logtenure pid_ce inc_quin1_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1Low
probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 0 & inc_quin1_xx_g1 != ., vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowShort
probit vote_ce logtenure pid_ce inc_quin3_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredQ3Mid
probit vote_ce logtenure pid_ce inc_mean_xx_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 1 & inc_quin3_xx_g1 != ., vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidShort

estout  PreferredQ1Low PreferredLowShort PreferredQ3Mid PreferredMidShort		using "$texdir/ca_cbevcomp_tableG.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_xx_g1 inc_quin1_xx_g1 inc_quin3_xx_g1 inc_top5_xx_g1)


probit vote_ce logtenure pid_ce inc_dist_quin1_g1 inc_top5_xx_g1 [pweight=natlweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1Low

estout  PreferredQ1Low		using "$texdir/ca_cbevcomp_tableH.tex",	$estout_options_w_se order(logtenure pid_ce inc_dist_quin1_g1 inc_top5_xx_g1)


gen model_country = "Canada"
gen model_name = ""
gen predicted_diff_mean_m = .
gen predicted_diff_mean_se = .
gen predicted_diff_t5_m = .
gen predicted_diff_t5_se = .
local counter = 1
foreach model in BartelsLogtenure PreferredLow PreferredQ1Low PreferredMid PreferredQ3Mid {
  estimates restore `model'
  summ inc_top5_xx_g1 if e(sample)
  local t5_mean =  r(mean)
  local t5_sd   =  r(sd)
  local t5_mean_sd = `t5_mean' + `t5_sd'
  margins, pwcompare at((asobserved) _all inc_top5_xx_g1 =(`t5_mean' `t5_mean_sd'))
  matrix pdiff_m = r(b_vs)
  matrix pdiff_v = r(V_vs)
  scalar pdiff_m = pdiff_m[1,1]
  scalar pdiff_se = pdiff_v[1,1]^(1/2)
  replace model_name = "`model'" if _n == `counter'
  replace predicted_diff_t5_m = pdiff_m if _n == `counter'
  replace predicted_diff_t5_se = pdiff_se if _n == `counter'
  // Now mean version
  if "`model'" == "BartelsLogtenure" | "`model'" == "PreferredLow" | "`model'" == "PreferredMid" {
    summ inc_mean_xx_g1 if e(sample)
    local mean_mean =  r(mean)
    local mean_sd   =  r(sd)
    local mean_mean_sd = `mean_mean' + `t5_sd'
    margins, pwcompare at((asobserved) _all inc_mean_xx_g1 =(`mean_mean' `mean_mean_sd'))
    matrix pdiff_m = r(b_vs)
    matrix pdiff_v = r(V_vs)
    scalar pdiff_m = pdiff_m[1,1]
    scalar pdiff_se = pdiff_v[1,1]^(1/2)
    replace model_name = "`model'" if _n == `counter'
    replace predicted_diff_mean_m = pdiff_m if _n == `counter'
    replace predicted_diff_mean_se = pdiff_se if _n == `counter'
  }
  // Increment counter
  local counter = `counter' + 1
}
keep model_country model_name predicted_diff_*
drop if model_name == ""
save "$gendatadir/ca_predicted_differences.dta", replace

